<template>
    <div class="default-main ba-table-box">
        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: '名称' })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />

        !-- 表单 -->
        <InfoDialog />
    </div>
</template>

<script setup lang="ts">
import { ref, provide } from 'vue'
import baTableClass from '/@/utils/baTable'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import InfoDialog from './info.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import { info } from '/@/api/backend/feedback/records'
import { buildJsonToElTreeData } from '/@/utils/common'

defineOptions({
    name: 'feedback/records',
})
let optButtons: OptButton[] = [
    {
        render: 'tipButton',
        name: 'info',
        title: '回复',
        text: '',
        type: 'primary',
        icon: 'fa fa-search-plus',
        class: 'table-row-info',
        disabledTip: false,
        click: (row: TableRow) => {
            infoButtonClick(row[baTable.table.pk!])
        },
    },
]
optButtons = optButtons.concat(defaultOptButtons(['delete']))
const { t } = useI18n()
const tableRef = ref()
const baTable = new baTableClass(
    new baTableApi('/admin/feedback.records/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: 'ID', prop: 'id', align: 'center', operator: false },
            { label: '类型', prop: 'type.name', align: 'center', operator: false },
            { label: '用户', prop: 'user.nickname', align: 'center', operator: false },
            { label: '内容', prop: 'content', align: 'center', operator: false, showOverflowTooltip: true },
            {
                label: '状态',
                prop: 'status',
                align: 'center',
                render: 'tag',
                custom: { 1: 'success', 0: 'danger' },
                replaceValue: { '1': '已回复', '0': '待回复' },
            },
            {
                label: t('Operate'),
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: optButtons,
                operator: false,
            },
        ],
        dblClickNotEditColumn: ['all'],
    }, {
        defaultItems: {
            status: '1',
            sort: 1,
        },
    },
    {
        onTableDblclick: ({ row }) => {
            infoButtonClick(row[baTable.table.pk!])
            return false
        }
    }
)

baTable.mount()
baTable.getIndex()
const infoButtonClick = (id: string) => {
    baTable.form.extend!['info'] = {}
    baTable.form.operate = 'Info'
    baTable.form.loading = true
    info(id).then((res) => {
        res.data.row.data = res.data.row.data
            ? [{ label: t('security.dataRecycleLog.Click to expand'), children: buildJsonToElTreeData(res.data.row.data) }]
            : []
        baTable.form.extend!['info'] = res.data.row
        baTable.form.loading = false
    })
}
provide('baTable', baTable)
</script>

<style scoped lang="scss"></style>
